package config

import (
	"embed"
	"fmt"
	"log"

	"gitee.com/tgodfather/misc/config"
	"gitee.com/tgodfather/misc/logger"
	"go.uber.org/zap"
)

//log "hms/util/logger"
//go:embed rs
var efs embed.FS

type ServiceConfig struct {
	ID              string `yaml:"id"`
	ServiceType     string `yaml:"server_type"`
	ServicePrefix   string `yaml:"server_prefix"`
	ServiceName     string `yaml:"server_name"`
	ServiceVersion  string `yaml:"server_version"`
	ServerAddress   string `yaml:"server_address"`
	ServerAdvertise string `yaml:"server_advertise"`
	RegistryType    string `yaml:"registry_type"`
	RegistryAddress string `yaml:"registry_address"`
	BrokerType      string `yaml:"broker_type"`
	BrokerAddress   string `yaml:"broker_address"`
	//工作流相关配置
	// WorkCenterConfig string `yaml:"workcenter_config"`
	// TaskCenterConfig string `yaml:"taskcenter_config"`

	Params map[string]string `yaml:"params"`
}

var serviceCfg = &ServiceConfig{
	ID:              "def",
	ServerAddress:   "0.0.0.0:10888",
	RegistryAddress: "0.0.0.0:2191",
	RegistryType:    "static",
}

func LoadConfig() *ServiceConfig {
	//加载全局变量配置
	// gv := globalconfig.GetGlobalInfo()

	// //预处理config.yaml
	// tmpDir := "tmp"
	// configFile := "config/config.yaml"
	// tmpConfigFile := gv.ConfigPrepareToDir(configFile, tmpDir)

	// data, err := ioutil.ReadFile(tmpConfigFile)
	// if nil != err {
	// 	//log.Error(" as.image load config error:", zap.Error(err))
	// 	fmt.Println(" as.image load config error:", err)
	// 	//return nil
	// } else {
	// 	yaml.Unmarshal(data, &serviceCfg)
	// 	//fmt.Println(" TaskCenter load config ", tc.cfg)
	// }

	// //预处理work.yaml 与  flow.yaml
	// serviceCfg.WorkCenterConfig = gv.ConfigPrepareToDir(serviceCfg.WorkCenterConfig, tmpDir)
	// serviceCfg.TaskCenterConfig = gv.ConfigPrepareToDir(serviceCfg.TaskCenterConfig, tmpDir)

	//log.Debug("Load config ", zap.Reflect("serviceCfg", serviceCfg))

	//需要保证只进来一次

	//暂时在此进行log初始化
	logcfg := &logger.LogConfig{
		ConsoleEnable: true,
		Filename:      "./log/api.log",
		MaxSize:       100,
		MaxBackups:    7,
		MaxAge:        30,
		Zap:           zap.NewProductionConfig(),
	}
	logger := logger.NewLogger("api", logcfg, true)
	log.SetOutput(logger)

	content, err := efs.ReadFile("rs/def.yaml")
	if nil == err {
		log.Println("content", string(content))
	} else {
		log.Println(err)
	}

	vc, _ := config.NewVConfig(config.WithDefConfigData(content, "yaml"))
	if rErr := vc.ReadConfigFile(config.WithConfigName("config"), config.WithConfigType("yaml")); nil != rErr {
		log.Println(rErr)
	}
	if uErr := vc.Unmarshal(serviceCfg); nil != uErr {
		log.Println(uErr)
	}

	err = vc.Unmarshal(serviceCfg)

	log.Println("===Load config", err, serviceCfg)

	//如果 没有设置serviceCfg.ServiceName，自己组装服务名
	if 0 >= len(serviceCfg.ServiceName) {
		serviceCfg.ServiceName = fmt.Sprintf("%s.%s",
			"hms",
			serviceCfg.ID)
	}

	return serviceCfg
}

func GetConfig() *ServiceConfig {
	return serviceCfg
}

//zk地址
func (cfg *ServiceConfig) GetRegistryAddress() string {
	return cfg.RegistryAddress
}

//kafka地址
func (cfg *ServiceConfig) GetBrokerAddress() string {
	return cfg.BrokerAddress
}

func (cfg *ServiceConfig) GetParam(key string) (string, bool) {
	value, ok := cfg.Params[key]
	return value, ok
}

//遍历参数,并执行操作处理函数
//函数 返回true 表示 退出循环
func (cfg *ServiceConfig) WalkParams(f func(key string, value string) bool) {
	for k, v := range cfg.Params {
		bQuit := f(k, v)
		if bQuit {
			break
		}
	}
}
